package com.yangc.stream.rabbit.producer;

import com.yangc.stream.rabbit.entity.DemoMessage;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.UUID;

@Service
public class DemoProducer {

    private final StreamBridge streamBridge;

    public DemoProducer(StreamBridge streamBridge) {
        this.streamBridge = streamBridge;
    }

    // 普通发送
    public void sendMessage(String content) {
        DemoMessage msg = new DemoMessage(UUID.randomUUID().toString(), content, Instant.now().toEpochMilli());
        streamBridge.send("producer-out-0", msg);
    }

    // 延迟发送
    public void sendDelayMessage(String content, int delayMs) {
        DemoMessage msg = new DemoMessage(UUID.randomUUID().toString(), content, Instant.now().toEpochMilli());
        streamBridge.send("producer-out-0", msg); // 先正常发送

        // 单独发到延迟 topic
        streamBridge.send("delayConsumer-in-0", org.springframework.messaging.support.MessageBuilder
                .withPayload(msg)
                .setHeader("x-delay", delayMs) // RabbitMQ 延迟插件关键header
                .build());
    }
}
